/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AgenciaListaform.java
 *
 * Created on 03/04/2010, 01:57:32 AM
 */

package agencia.form;

import agencia.Agencia;
import agencia.controller.AgenciaController;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author elvis
 */
public class AgenciaListaform extends javax.swing.JInternalFrame {

    private AgenciaController agenciaController;
    private JDesktopPane jdesktopPane;

    public JDesktopPane getJdesktopPane() {
        return jdesktopPane;
    }
    public void setJdesktopPane(JDesktopPane jdesktopPane) {
        this.jdesktopPane = jdesktopPane;
    }
    public AgenciaController getAgenciaController() {
        return agenciaController;
    }
    public void setAgenciaController(AgenciaController agenciaController) {
        this.agenciaController = agenciaController;
    }
    /** Creates new form AgenciaListaform */
    public AgenciaListaform() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblAgencias = new javax.swing.JTable();
        btnnuevo = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btneliminar = new javax.swing.JButton();
        pnAgencia = new javax.swing.JPanel();
        txtnombre = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        btnDetalle = new javax.swing.JButton();

        setClosable(true);

        tblAgencias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Responsable"
            }
        ));
        jScrollPane1.setViewportView(tblAgencias);

        btnnuevo.setText("Nuevo");
        btnnuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnuevoActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btneliminar.setText("Eliminar");
        btneliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneliminarActionPerformed(evt);
            }
        });

        pnAgencia.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de Agencia"));
        pnAgencia.setToolTipText("Datos de Agencia");
        pnAgencia.setName("Agencia"); // NOI18N

        jLabel1.setText("Nombre");

        jButton1.setText("Buscar");

        javax.swing.GroupLayout pnAgenciaLayout = new javax.swing.GroupLayout(pnAgencia);
        pnAgencia.setLayout(pnAgenciaLayout);
        pnAgenciaLayout.setHorizontalGroup(
            pnAgenciaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnAgenciaLayout.createSequentialGroup()
                .addGroup(pnAgenciaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnAgenciaLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(28, 28, 28)
                        .addComponent(txtnombre, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE))
                    .addGroup(pnAgenciaLayout.createSequentialGroup()
                        .addGap(98, 98, 98)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        pnAgenciaLayout.setVerticalGroup(
            pnAgenciaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnAgenciaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnAgenciaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addComponent(jButton1))
        );

        btnDetalle.setText("Detalle");
        btnDetalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetalleActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnAgencia, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnnuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btneliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDetalle, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDetalle, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                    .addComponent(btneliminar, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                    .addComponent(btnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnnuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnnuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnuevoActionPerformed
        // TODO add your handling code here:
         AgenciaDatosform ad = new AgenciaDatosform();
         ad.setAgenciaController(agenciaController);
         ad.setAgenciaListaform(this);
         ad.setAgencia(new Agencia());
         ad.setAccion(AgenciaDatosform.ACCION_NUEVO);
         getJdesktopPane().add(ad);
         ad.show();
    }//GEN-LAST:event_btnnuevoActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        // TODO add your handling code here:
         if (tblAgencias.getSelectedRow()!=-1){
            int cod = (Integer)tblAgencias.getValueAt( tblAgencias.getSelectedRow(), 0);
            Agencia a = agenciaController.obtenerAgencia(cod);
            AgenciaDatosform ad = new AgenciaDatosform();
            ad.setAgenciaController(agenciaController);
            ad.setAgenciaListaform(this);
             ad.setAgencia(a);
             ad.setAccion(AgenciaDatosform.ACCION_MODIFICAR);
             getJdesktopPane().add(ad);
             ad.show();
         }else{
          JOptionPane.showMessageDialog(rootPane, "Debe seleccionar el cliente a eliminar");
         }
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btneliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneliminarActionPerformed
         if (tblAgencias.getSelectedRow()!=-1){
            int cod = (Integer)tblAgencias.getValueAt( tblAgencias.getSelectedRow(), 0);
            agenciaController.eliminarAgencia(cod);
            pintagrilla();
         }else{
          JOptionPane.showMessageDialog(rootPane, "Debe seleccionar el cliente a eliminar");
         }
    }//GEN-LAST:event_btneliminarActionPerformed

    private void btnDetalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetalleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnDetalleActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDetalle;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btneliminar;
    private javax.swing.JButton btnnuevo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnAgencia;
    private javax.swing.JTable tblAgencias;
    private javax.swing.JTextField txtnombre;
    // End of variables declaration//GEN-END:variables

    
    
    public void pintagrilla(){
        DefaultTableModel tabla = (DefaultTableModel) tblAgencias.getModel();
        tabla.setRowCount(0);
        List<Agencia> examenes = agenciaController.listaAgencia();
        for (Agencia examen : examenes) {
            Object[] fila = new Object[5];
            fila[0] = examen.getCodigo();
            fila[1] = examen.getNombre();
            fila[2] = examen.getResponsable();
            tabla.addRow(fila);
        }
    }

    public void iniciarForm() {
        pintagrilla();
    }

    
}
